﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace MTQ.BLL
{
    public class HttpHelper
    {
        public static string GetWebApiData(string postData, string uriStr)
        {
            try
            {
                HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create(uriStr);
                //postData =             
                UTF8Encoding encoding = new UTF8Encoding(); //new ASCIIEncoding();            
                byte[] data = encoding.GetBytes(postData);
                requestScore.Method = "Post";
                requestScore.ContentType = "application/x-www-form-urlencoded"; requestScore.ContentLength = data.Length;
                requestScore.KeepAlive = true;
                //使用cookies            
                //requestScore.CookieContainer = ...;            
                Stream stream = requestScore.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                HttpWebResponse responseSorce = (HttpWebResponse)requestScore.GetResponse();
                StreamReader reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.GetEncoding("GBK"));
                string content = reader.ReadToEnd();

                requestScore = null;
                responseSorce.Close();
                responseSorce = null;
                reader = null;
                stream = null;
                return content;
            }
            catch
            {
                Console.WriteLine("数据异常" + uriStr);
                return null;
            }
        }


        public static string GetWebPageData(string strUrl)
        {

            string rl;
            try
            {
                WebRequest request = WebRequest.Create(strUrl);
                request.Method = "GET";
                WebResponse Response = request.GetResponse();
                Stream resStream = Response.GetResponseStream();
                StreamReader sr = new StreamReader(resStream, Encoding.Default);
                StringBuilder sb = new StringBuilder();
                while ((rl = sr.ReadLine()) != null)
                {
                    sb.Append(rl);
                }
                return sb.ToString();

            }
            catch
            {
                return string.Empty;
            }

        }
        public static string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
           // request.CookieContainer = cookie;
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

          //  response.Cookies = cookie.GetCookies(response.ResponseUri);
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

        public static string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        } 


    }
}
